﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UserInterfaceLayer.User
{
    public partial class Room : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblFailure.Visible = false;
            lblInvalidInput.Visible = false;
            lblSuccessful.Visible = false;
            lblNewItem.Visible = false;
            lblDeletedItem.Visible = false;
            btnDelete.Visible = false;
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            string strRoomID = Request.QueryString["RoomID"];
            DataAccessLayer.linqDatabaseDataContext db = new DataAccessLayer.linqDatabaseDataContext();
            FillDropDowns(db);
            if (strRoomID != "add")
            {
                int RoomID;
                if (!int.TryParse(strRoomID, out RoomID))
                    Response.Redirect("Room.aspx?RoomID=add");
                DataAccessLayer.Room Room = DataAccessLayer.Room.FindByRoomID_IncludeDeleted(db, RoomID);
                if (Room == null)
                    Response.Redirect("Room.aspx?RoomID=add");
                FromDatabase(Room);
                btnDelete.Visible = true;
                lvHistory.DataSource = BusinessLogicLayer.DataObject.Room.Convert(DataAccessLayer.Room.HistoryToListByRoomID(db, RoomID));
                lvHistory.DataBind();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                int UserID = (int)Session["UserID"];
                string strRoomID = Request.QueryString["RoomID"];
                int OrderBy;
                if (!int.TryParse(txtOrderBy.Text, out OrderBy))
                {
                    lblInvalidInput.Visible = true;
                    return;
                }
                if (strRoomID != "add")
                {
                    int RoomID = int.Parse(strRoomID);
                    DataAccessLayer.linqDatabaseDataContext db = new DataAccessLayer.linqDatabaseDataContext();
                    //DataAccessLayer.Room Room = DataAccessLayer.Room.FindByRoomID_IncludeDeleted(db, RoomID);
                    DataAccessLayer.Room NewRoom = new DataAccessLayer.Room();
                    NewRoom.RoomID = RoomID;
                    NewRoom.EditorUserID = UserID;
                    NewRoom.Timestamp = DateTime.Now;
                    ToDatabase(NewRoom);
                    db.Rooms.InsertOnSubmit(NewRoom);
                    db.SubmitChanges();
                    lblSuccessful.Visible = true;
                }
                else
                {
                    DataAccessLayer.linqDatabaseDataContext db = new DataAccessLayer.linqDatabaseDataContext();
                    DataAccessLayer.sRoom sRoom = new DataAccessLayer.sRoom();
                    db.sRooms.InsertOnSubmit(sRoom);
                    db.SubmitChanges();
                    DataAccessLayer.Room NewRoom = new DataAccessLayer.Room();
                    NewRoom.RoomID = sRoom.ID;
                    NewRoom.EditorUserID = UserID;
                    NewRoom.Timestamp = DateTime.Now;
                    ToDatabase(NewRoom);
                    db.Rooms.InsertOnSubmit(NewRoom);
                    db.SubmitChanges();
                    lblNewItem.Visible = true;
                    btnSave.Visible = false;
                }
            }
            catch (Exception)
            {
                lblFailure.Visible = true;
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int UserID = (int)Session["UserID"];
                string strRoomID = Request.QueryString["RoomID"];
                int RoomID = int.Parse(strRoomID);
                DataAccessLayer.linqDatabaseDataContext db = new DataAccessLayer.linqDatabaseDataContext();
                //DataAccessLayer.Room Room = DataAccessLayer.Room.FindByRoomID_IncludeDeleted(db, RoomID);
                DataAccessLayer.Room NewRoom = new DataAccessLayer.Room();
                NewRoom.RoomID = RoomID;
                NewRoom.EditorUserID = UserID;
                NewRoom.Timestamp = DateTime.Now;
                NewRoom.IsDeleted = true;
                ToDatabase(NewRoom);
                db.Rooms.InsertOnSubmit(NewRoom);
                db.SubmitChanges();
                lblSuccessful.Visible = true;
            }
            catch (Exception)
            {
                lblFailure.Visible = true;
            }
        }

        public void FromDatabase(DataAccessLayer.Room Room)
        {
            txtDescription.Text = Room.Description;
            txtName.Text = Room.Name;
            txtOrderBy.Text = Room.OrderBy.ToString();
            ddlFloor.SelectedValue = Room.FloorID.ToString();
            ddlStatus.SelectedValue = Room.RoomStatusID.ToString();
            lblDeletedItem.Visible = Room.IsDeleted;
        }

        public void ToDatabase(DataAccessLayer.Room Room)
        {
            Room.Description = txtDescription.Text;
            Room.Name = txtName.Text;
            Room.OrderBy = int.Parse(txtOrderBy.Text);
            Room.FloorID = int.Parse(ddlFloor.SelectedValue);
            Room.RoomStatusID = int.Parse(ddlStatus.SelectedValue);
        }

        public void FillDropDowns(DataAccessLayer.linqDatabaseDataContext db)
        {
            ddlFloor.DataSource = DataAccessLayer.Floor.ToList(db);
            ddlFloor.DataTextField = "Name";
            ddlFloor.DataValueField = "ID";
            ddlFloor.DataBind();
        }
    }
}